package com.ruoyi.system.constant;

import com.ruoyi.system.domain.csqaq.Alteration;

public class MessageGenerator {
    public static String generateMessage(Alteration alteration) {
        StringBuilder message = new StringBuilder();

        // 构建消息
        message.append("📦 商品：").append(alteration.getName()).append("\n\n");
        message.append("🔄 价格监控提醒 | 今天 ").append(alteration.getCreatedAt()).append("\n\n");
        message.append("🏷️ 商品ID：").append(alteration.getGoodid()).append("\n\n");

        // 价格变化部分
        message.append("💰 价格变化：\n\n");
        // 在售价格变动
        double sellPriceChange = alteration.getOriginSellPrice() - alteration.getTargetSellPrice();
        message.append("原售价：").append(alteration.getOriginSellPrice()).append(" → 现售价：").append(alteration.getTargetSellPrice()).append(getGrowthOrDeclineEmoji(sellPriceChange)).append(" ").append(String.format("%.2f", Math.abs(sellPriceChange))).append("\n\n");

        // 求购价格变动
        double buyPriceChange = alteration.getOriginBuyPrice() - alteration.getTargetBuyPrice();
        message.append("原求购价：").append(alteration.getOriginBuyPrice()).append(" → 现求购价：").append(alteration.getTargetBuyPrice()).append(getGrowthOrDeclineEmoji(buyPriceChange)).append(" ").append(String.format("%.2f", Math.abs(buyPriceChange))).append("\n\n");

        // 库存变化部分
        message.append("📊 库存变化：\n\n");

        // 在售数量变化
        long sellNumChange = alteration.getTargetSellNum() - alteration.getOriginSellNum();
        String sellTrend = getTrendEmoji(sellNumChange);
        message.append("在售数量：").append(alteration.getOriginSellNum()).append(" → ").append(alteration.getTargetSellNum()).append(" ").append(sellTrend).append(" ").append(Math.abs(sellNumChange)).append("件\n\n");

        // 求购数量变化
        long buyNumChange = alteration.getTargetBuyNum() - alteration.getOriginBuyNum();
        String buyTrend = getTrendEmoji(buyNumChange);
        message.append("求购数量：").append(alteration.getOriginBuyNum()).append(" → ").append(alteration.getTargetBuyNum()).append(" ").append(buyTrend).append(" ").append(Math.abs(buyNumChange)).append("件\n\n");

        message.append("🔗 监控平台：").append(getPlatform(alteration.getMonitorKey()));

        return message.toString();
    }

    private static String getGrowthOrDeclineEmoji(double change) {
        if (change == 0) {
            return " ↔️";
        } else {
            return change > 0 ? " 📉" : " 📈";
        }
    }

    private static String getPlatform(String monitorKey) {
        switch (monitorKey) {
            case "buff_sell_price":
                // 在售底价
                return "Buff";
            case "buff_sell_num":
                // 在售数量
                return "Buff";
            case "buff_buy_num":
                // 求购数量
                return "Buff";
            case "yyyp_sell_price":
                // 在售底价
                return "悠悠有品";
            case "yyyp_sell_num":
                // 在售数量
                return "悠悠有品";
            case "yyyp_buy_num":
                // 求购数量
                return "悠悠有品";
            default:
                return "未知平台";
        }
    }

    private static String getTrendEmoji(long change) {
        if (change > 0) {
            return " 📈";
        } else if (change < 0) {
            return " 📉";
        } else {
            return " ↔️";
        }
    }
}
